package io.github.dsl.runner

import io.github.dsl.conf.RuleScript
import org.codehaus.groovy.control.CompilerConfiguration

class GroovyScriptRunner {

  static Script getScript(String rootDir, String scriptName) {
    String filePath = rootDir + "/" + scriptName;
    return getScriptByPath(filePath);
  }

  static Script getScriptByPath(String filePath) {
    def compilerConfiguration = new CompilerConfiguration()
    compilerConfiguration.scriptBaseClass = RuleScript.class.getName()
    def groovyShell = new GroovyShell(GroovyScriptRunner.class.classLoader, compilerConfiguration)

    try {
      File file = new File(filePath)
      return groovyShell.parse(file.text)
    } catch (IOException e) {
      throw new RuntimeException("getScript error,path: " + filePath, e);
    }
  }
}
